<?php

class Gate extends Controller{
	function Gate(){
		parent::Controller();
	}
	
	function index(){
		$data=array();
		if($this->session->userdata('logged_in')){
			$id = $this->session->userdata('id');
			if($this->session->userdata('type')=="teacher"){
				if ($this->session->userdata('teacherstatus') == "pending"){
					redirect ('teacher/edit_profile');
				} else {
					redirect('teacher');			
				}
			} elseif($this->session->userdata('type')=="student"){
				if ($this->session->userdata('regstatus')  == "On Trial"){
					redirect('student/edit_profile');
				} else{
					redirect('student');
				}
			} elseif($this->session->userdata('type')=="admin"){
				redirect('admin');
			} else {
				echo "An error occured.";
			}
		}
		else{
			$gate['body'] = $this->load->view('gate/home', $data, TRUE);
			$gate['login'] = $this->load->view('gate/login', $data, TRUE);
			$this->load->view('home', $gate);
		}	
	}
	
	function register(){
		$data = array();
		$rules = array();
		
		$rules['email'] = "required|valid_email";
		$rules['email_conf'] = "matches[email]";
		$rules['password'] = "required|min_length[8]|alpha_dash";
		$rules['password_conf'] = "matches[password]";

		$this->validation->set_rules($rules);
		$this->validation->set_error_delimiters('<div id="error">', '</div>');
		
		$fields = array();
		$fields['email'] = "E-mail";
		$fields['email_conf'] = "E-mail Confirmaton";
		$fields['password'] = "Password";
		$fields['password_conf'] = "Password Confirmation";
		$this->validation->set_fields($fields);
		
		if($this->validation->run()==TRUE){
			$new['email'] = $this->input->post('email');
			$salt = random_string('alnum', 40);
			$new['salt'] = $salt;
			$password = $this->input->post('password');
			$hash = sha1($salt.$password);
			$new['password'] = $hash;
			$new['activation_code'] = random_string('alnum', 50);
			$new['usertype'] = $this->input->post('usertype');
			$new['reg_date'] = date("Y-n-j");
			$this->Register->insert('signup', $new);

			$this->_send_mail($this->input->post('email'), $new['activation_code']);
			$this->session->set_flashdata('message', 'Please check your email to confirm your smart UP account.');			
			redirect('gate');
		}
		$gate['login'] = $this->load->view('gate/login', $data, TRUE);
		$gate['body'] = $this->load->view('gate/register', $data, TRUE);
		$this->load->view('home', $gate);
	}

	function _send_mail($email, $activation_code){

		$user = $this->Register->find_unconfirmed_user($email);
		$link = site_url('gate/confirm/'.$user['userid'].'/'.$activation_code);

		$this->email->from('smartuptutorial@gmail.com','Smart UP!');  
		$this->email->to($email); 
		$this->email->subject('Smart UP! account confirmation.');
		$this->email->message('Please click the following link to confirm your account. ' .$link);	

		$this->email->send();	
	}

	function confirm($id = 0, $activation_code = ''){
		$result = $this->Register->confirm($id, $activation_code);
		if($result['usertype']=="teacher"){
			if($result!=FALSE){
				$new = array(
						'email' => $result['email'], 
						'password' => $result['password'], 
						'teacherstatus' => 'pending',
						'salt' => $result['salt'],							
						);
				$this->Register->insert('teachers', $new);
				$this->Register->delete($this->uri->segment(3));
				$this->session->set_flashdata('message', 'Account confirmed. Please log in to update your profile.');
			}
		} elseif($result['usertype']=="student"){
			if($result!=FALSE){
				$new = array(
						'email' => $result['email'], 
						'password' => $result['password'], 
						'regstatus' => 'On Trial',
						'salt' => $result['salt'],							
						);
				$this->Register->insert('students', $new);
				$this->Register->delete($this->uri->segment(3));
				$this->session->set_flashdata('message', 'Account confirmed. Please log in to update your profile.');
			}
		} redirect('gate');
	}
	
	function login(){
		$data = array();
		$gate['login'] = $this->load->view('gate/login', $data, TRUE);
		$this->load->view('gate/login', $gate);
		
		$email = $this->input->post('email');
		$password = $this->input->post('password');
		$usertype = $this->input->post('usertype');
			
		if ($usertype == 'student'){
			$salt = $this->Register->getSalt($email, 'students');
			$user = $this->Register->auth('students', $email, $password, $salt);
			if($user==FALSE){
				$this->session->set_flashdata('message', 'An error occured, please try again.');
			} else {
				$sessionData = array(
					'email' => $user['email'],
					'userid' => $user['userid'],
					'logged_in' => TRUE,
					'type' => 'student',
					'regstatus'=> $user['regstatus'],
					'regtime' => $user['regtime'],
					'last_login'=>$user['last_login'],
				);
				$this->session->set_userdata($sessionData);
			} 

		} elseif($usertype=='teacher'){
			$salt = $this->Register->getSalt($email, 'teachers');
			$user = $this->Register->auth('teachers', $email, $password, $salt);
			if($user == FALSE){
				$this->session->set_flashdata('message', 'An error occured, please try again.');
			} else {
				$sessionData = array(
					'email' => $user['email'],
					'userid' => $user['userid'],
					'logged_in' => TRUE,
					'type' => 'teacher',
					'teacherstatus'=>$user['status'],
					'regtime' => $user['regtime'],
					'last_login'=>$user['last_login'],
				);
			$this->session->set_userdata($sessionData);
			} 
		} else {
			$salt = $this->Register->getSalt($email, 'signup');
			$user = $this->Register->auth('signup', $email, $password, $salt);
			if($user==FALSE){
				$this->session->set_flashdata('message', 'An error occured, please try again.');
			} elseif($user['usertype']=="admin") {
				$sessionData = array(
					'email' => $user['email'],
					'logged_in' => TRUE,
					'type' => 'admin',
					'profilestat' => 'pending',
					);
			$this->session->set_userdata($sessionData);
			} 
		} redirect('gate');
	}

	function logout(){
		$this->session->sess_destroy();
		redirect('gate');
	}

	function offerings(){
			$data = array();
			$gate['login'] = $this->load->view('gate/login', $data, TRUE);
			$gate['body'] = $this->load->view('gate/offerings', $data, TRUE);
			$this->load->view('home', $gate);
	}

	function about_us(){
			$data = array();
			$gate['login'] = $this->load->view('gate/login', $data, TRUE);
			$gate['body'] = $this->load->view('gate/about_us', $data, TRUE);
			$this->load->view('home', $gate);
	}

	function query(){
			$data = array();
			$gate['login'] = $this->load->view('gate/login', $data, TRUE);
			$gate['body'] = $this->load->view('gate/query', $data, TRUE);
			$this->load->view('home', $gate);
	}
	
}

?>
